package com.xyzwps.server.paimon.ui.mvc

import com.xyzwps.server.paimon.error.Errors
import com.xyzwps.server.paimon.model.entity.User
import com.xyzwps.server.paimon.model.service.UserDomainService
import com.xyzwps.server.paimon.ui.HttpResponse
import com.xyzwps.server.paimon.value.UserId
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping(Apis.BASE + "/users")
class UserController(private val userDomainService: UserDomainService) {

    /**
     * Get user by ID
     *
     * @param id User ID
     * @return User information or 404 error
     */
    @GetMapping("/{id}")
    fun getUserById(@PathVariable id: Long): HttpResponse<User> {
        val user = userDomainService.getById(UserId(id)) ?: throw Errors.USER_NOT_FOUND.toEx()
        return HttpResponse(user)
    }
}
